package com.alphagen.fuelprogram.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alphagen.fuelprogram.service.FileUploadService;
import com.alphagen.fuelprogram.validator.FileValidator;
import com.alphagen.fuelprogram.vo.FindRoute;
import com.alphagen.fuelprogram.vo.GasolineStation;
import com.alphagen.fuelprogram.vo.UploadedFile;

@Controller
@SessionAttributes({ "parsedList" })
public class AppController {

	@Autowired
	FileValidator fileValidator;

	@Autowired
	FileUploadService fileUploadService;

	@RequestMapping(value = {"/", "/home" }, method = RequestMethod.GET)
	public ModelAndView home(
			@ModelAttribute("uploadedFile") UploadedFile uploadedFile,
			BindingResult result) {
		ModelAndView model = new ModelAndView();
		model.setViewName("home");
		return model;
	}

	@RequestMapping("/uploadFile")
	public String fileUploaded(
			@ModelAttribute("uploadedFile") UploadedFile uploadedFile,
			BindingResult result, Map model) {
		InputStream inputStream = null;
		List<GasolineStation> parsedInput = new ArrayList<GasolineStation>();
		MultipartFile file = uploadedFile.getFile();
		fileValidator.validate(uploadedFile, result);

		String fileName = file.getOriginalFilename();

		if (result.hasErrors()) {
			return "home";
		}

		try {
			inputStream = file.getInputStream();
			JSONArray jsonArray = fileUploadService
					.convertPDFToJSON(inputStream);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				GasolineStation gasoline = new GasolineStation();
				if (jsonObject.getString("DATE") != null
						&& !jsonObject.getString("DATE").equals("null")) {
					gasoline.setInfoDate(jsonObject.getString("DATE"));
					gasoline.setSt(jsonObject.getString("ST"));
					gasoline.setCity(jsonObject.getString("CITY"));
					gasoline.setPPG(jsonObject.getString("PPG"));
					gasoline.setName(jsonObject.getString("NAME"));
					gasoline.setAddress(jsonObject.getString("ADDRESS"));
					gasoline.setDirections(jsonObject.getString("DIRECTIONS"));
					if (gasoline.isDataValid()) {
						parsedInput.add(gasoline);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		model.put("parsedList", parsedInput);

		return "redirect:parsedList";
	}

	@RequestMapping("/parsedList")
	public String parsedList(Map model) {
		List<GasolineStation> parsedList = (List<GasolineStation>) model
				.get("parsedList");
		if (parsedList != null && parsedList.size() > 0) {
			model.put("parsedList", parsedList);
		} else {
			return "redirect:home";
		}
		return "parsedList";
	}
	

	@RequestMapping("/findRoute")
	public String findRoute(Map model) {
		
		List<GasolineStation> parsedList = (List<GasolineStation>) model
				.get("parsedList");
		if (parsedList != null && parsedList.size() > 0) {
			FindRoute findRoute = new FindRoute();
			model.put("findRoute", findRoute);
		} else {
			return "redirect:home";
		}
		return "findRoute";
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/showRoute")
	public String showRoute(@ModelAttribute("findRoute") FindRoute findRoute,
			BindingResult result, Map model) {
		List<GasolineStation> parsedList = (List<GasolineStation>) model
				.get("parsedList");
		if (parsedList != null && parsedList.size() > 0) {
			model.put("parsedList", parsedList);
			model.put("findRoute", findRoute);
			
		} else {
			return "redirect:home";
		}
		return "showRoute";
	}

	@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
	public ModelAndView login() {

		ModelAndView model = new ModelAndView();
		model.addObject("title",
				"Spring Security 3.2.3 Hello World Application");
		model.addObject("message", "Welcome Page !");
		model.setViewName("login");
		return model;

	}

	@RequestMapping(value = "/protected", method = RequestMethod.GET)
	public ModelAndView protectedPage() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security 3.2.3 Hello World");
		model.addObject("message",
				"This is protected page - Only for Administrators !");
		model.setViewName("protected");
		return model;

	}

	@RequestMapping(value = "/confidential", method = RequestMethod.GET)
	public ModelAndView superAdminPage() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security 3.2.3 Hello World");
		model.addObject("message",
				"This is confidential page - Need Super Admin Role !");
		model.setViewName("protected");

		return model;

	}

}
